/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.test;

import com.wtkj.vo.dzzf.base.kfwd.VehicleInfo;
import com.wttech.dzzf.check.moduleSet.CheckVehicleUpload_9;
import com.wttech.dzzf.process.constant.UserConstants;
import com.wttech.dzzf.process.util.BusinessUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.wttech.dzzf.check.moduleSet.CheckVehicleUpload_9.*;

/**
 * 车辆信息检测
 *
 * @author MaShibo
 * @date 2017/12/6.
 */
@Service
public class VehicleInfoCheck {

	private static final Logger VEHICLE = LoggerFactory.getLogger("Vehicle");

	void checkVehicle(VehicleInfo vehicleInfo) {
		String vehicleInfoId = StringUtils.trim(vehicleInfo.getVehicleInfoId());

		CheckVehicleUpload_9 check = new CheckVehicleUpload_9();

		String id = vehicleInfo.getVehPlate() + UserConstants.DEFAULT_VEHICLE_CHAR + vehicleInfo.getVehColor();
		Integer type = vehicleInfo.getVehType();
		String ownerName = vehicleInfo.getOwnerName();
		Integer ownerIdType = BusinessUtils.convertUserIdType(vehicleInfo.getOwnerIdType(), UserConstants.OLD_OWNERTYPE_USER);
		String ownerIdNum = vehicleInfo.getOwnerIdNum();
		String ownerTel = vehicleInfo.getOwnerTel();
		String address = vehicleInfo.getAddress();
		String contact = vehicleInfo.getContact();
		String vehicleType = vehicleInfo.getCertifiVehType();
		String vehicleModel = vehicleInfo.getBrand();
		Integer userCharacter = vehicleInfo.getUseCharacter();
		String vin = vehicleInfo.getVehiclelicNO();
		String engineNum = vehicleInfo.getVehicleEngineNO();
		Date registerDate = vehicleInfo.getRegisterDate();
		Date issueDate = vehicleInfo.getIssueDate();
		String fileNum = vehicleInfo.getFileNum();
		Integer approvedCount = vehicleInfo.getSeats();
		Integer totalMass = vehicleInfo.getWeight();
		Integer maintenanceMass = vehicleInfo.getCurbWeight();
		Integer permittedWeight = vehicleInfo.getLoadLimit();
		Integer vehlength = vehicleInfo.getVehLength();
		Integer vehWidth = vehicleInfo.getVehWidth();
		Integer vehHeight = vehicleInfo.getVehHeight();

		Integer permittedTowWeight = vehicleInfo.getPermittedTowWeight();
		String testRecord = vehicleInfo.getTestRecord();
		Integer wheelCount = vehicleInfo.getWheelCnt();
		Integer axleCount = vehicleInfo.getAxisCnt();
		Integer axleDistance = vehicleInfo.getWheelBase();
		String axisType = vehicleInfo.getAxisType();

		check.setOwnerIdType(ownerIdType);
		check.setRegisteredTypeTemp(UserConstants.REGISTEREDTYPE_OFFLINE);

		String result;

		result = check.checkId(id);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，车牌号【{}】，车牌颜色【{}】错误【{}】", new Object[]{vehicleInfoId, vehicleInfo.getVehPlate(), vehicleInfo.getVehColor(), result});
		}

		result = check.checkType(type);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，车型【{}】错误【{}】", new Object[]{vehicleInfoId, type, result});
		}

		result = checkOwnerName(ownerName);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，机动车所有人名称【{}】错误【{}】", new Object[]{vehicleInfoId, ownerName, result});
		}

		result = checkOwnerIdType(ownerIdType);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，机动车所有人证件类型【{}】错误【{}】", new Object[]{vehicleInfoId, ownerIdType, result});
		}

		result = check.checkOwnerIdNum(ownerIdNum);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，机动车所有人证件号码【{}】错误【{}】", new Object[]{vehicleInfoId, ownerIdNum, result});
		}

		result = checkOwnerTel(ownerTel);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】,所有人联系方式【{}】错误【{}】", new Object[]{vehicleInfoId, ownerTel, result});
		}

		result = checkAddress(address);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】,所有人联系地址【{}】错误【{}】", new Object[]{vehicleInfoId, address, result});
		}

		result = check.checkContact(contact);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，指定联系人姓名【{}】错误【{}】", new Object[]{vehicleInfoId, contact, result});
		}

		result = checkVehicleType(vehicleType);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，行驶证车辆类型【{}】错误【{}】", new Object[]{vehicleInfoId, vehicleType, result});
		}

		result = checkVehicleModel(vehicleModel);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，行驶证品牌型号【{}】错误【{}】", new Object[]{vehicleInfoId, vehicleType, result});
		}

		result = checkUseCharacter(userCharacter);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】， 车辆使用性质【{}】错误【{}】", new Object[]{vehicleInfoId, userCharacter, result});
		}

		result = check.checkVIN(vin);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，车辆识别代号【{}】错误【{}】", new Object[]{vehicleInfoId, vin, result});
		}

		result = checkEngineNum(engineNum);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，车辆发动机号【{}】错误【{}】", new Object[]{vehicleInfoId, engineNum, result});
		}

		result = checkRegisterDate(registerDate);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，注册日期【{}】错误【{}】", new Object[]{vehicleInfoId, registerDate, result});
		}

		result = checkIssueDate(issueDate);
		if (needOutput(result)) {
			VEHICLE.info("车辆细心【{}】， 发证日期【{}】错误【{}】", new Object[]{vehicleInfoId, issueDate, result});
		}

		result = checkFileNum(fileNum);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，档案编号【{}】错误【{}】", new Object[]{vehicleInfoId, fileNum, result});
		}

		result = checkApprovedCount(approvedCount);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，核定载人数【{}】错误【{}】", new Object[]{vehicleInfoId, approvedCount, result});
		}

		result = checkTotalMass(totalMass);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，总质量【{}】错误【{}】", new Object[]{vehicleInfoId, totalMass, result});
		}

		result = checkMaintenanceMass(maintenanceMass);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，整备质量【{}】错误【{}】", new Object[]{vehicleInfoId, maintenanceMass, result});
		}

		result = checkPermittedWeight(permittedWeight);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，核定载质量【{}】错误", new Object[]{vehicleInfoId, permittedWeight, result});
		}

		if (vehHeight != null && vehWidth != null && vehlength != null) {
			String outsideDimensions = vehlength + "X" + vehWidth + "X" + vehHeight;
			result = checkOutsideDimensions(outsideDimensions);
			if (needOutput(result)) {
				VEHICLE.info("车辆信息【{}】，外廓尺寸【{}】错误【{}】", new Object[]{vehicleInfoId, outsideDimensions, result});
			}
		}

		result = checkPermittedTowWeight(permittedTowWeight);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】， 准牵引质量【{}】错误【{}】", new Object[]{vehicleInfoId, permittedTowWeight, result});
		}

		result = checkTestRecord(testRecord);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，检验记录【{}】错误【{}】", new Object[]{vehicleInfoId, testRecord, result});
		}

		result = checkWheelCount(wheelCount);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，车轮数【{}】错误【{}】", new Object[]{vehicleInfoId, wheelCount, result});
		}

		result = checkAxleCount(axleCount);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，车轴数【{}】错误【{}】", new Object[]{vehicleInfoId, axleCount, result});
		}

		result = checkAxleDistance(axleDistance);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，轴距【{}】错误【{}】", new Object[]{vehicleInfoId, axleDistance, result});
		}

		result = checkAxisType(axisType);
		if (needOutput(result)) {
			VEHICLE.info("车辆信息【{}】，轴型【{}】错误【{}】", new Object[]{vehicleInfoId, axisType, result});
		}
	}

	private boolean needOutput(String result) {
		return !StringUtils.equalsIgnoreCase(result, UserConstants.RIGHT);
	}
}
